package com.java.socket;

import java.io.*;
import java.net.URL;
import java.net.URLConnection;

public class URLClient {
	public static void main(String[] args) throws Exception
	{
		URL url = new URL("http://java.sun.com:80/docs/books/tutorial/index.html#DOWN");
		String protocol = url.getProtocol();
		String host = url.getHost();
		String file = url.getFile();
		int port = url.getPort();
		String ref = url.getRef();
		System.out.println(protocol + ", " + host + ", " + file + ", " + port
				+ ", " + ref);
	}

	public static void url() throws Exception {
		URL url = new URL("http://www.infoq.com");
		URLConnection conn = url.openConnection();
		InputStream is = conn.getInputStream();
		OutputStream os = new FileOutputStream("c:\\infoq.txt");
		byte[] buffer = new byte[2048];
		int length = 0;
		while(-1 != (length = is.read(buffer, 0, buffer.length))){
			os.write(buffer, 0, length);
		}
		is.close();
		os.close();
	}

	public static void url2() throws Exception{
		URL url = new URL("http://www.csdn.net");
//		URLConnection conn = url.openConnection();
//
//		InputStream is = conn.getInputStream();
		InputStream is = url.openStream();
		OutputStream os = new FileOutputStream("c:\\infoq.html");
		byte[] buffer = new byte[2048];
		int length = 0;
		while(-1 != (length = is.read(buffer, 0, buffer.length))){
			os.write(buffer, 0, length);
		}
		is.close();
		os.close();
	}

	public static void url3() throws Exception{
		URL url = new URL("http://www.shengsiyuan.com");
		BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
		String line = null;
		while(null != (line = br.readLine())){
			System.out.println(line);
		}
		br.close();
	}
}
